package com.yihukurama.sysbase.controller;


import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import com.yihukurama.base.app.cache.RedisUtils;
import com.yihukurama.base.framework.web.dto.Request;
import com.yihukurama.base.framework.web.dto.Result;
import com.yihukurama.sysbase.controller.dto.CodeDTO;
import com.yihukurama.sysbase.framework.domain.User;
import com.yihukurama.sysbase.sysservice.IPublicApi;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.UUID;

/**
 * 说明： 档案模块接口
 *
 * @Author yihukurma
 * @date Created in 下午 1:21 2023/7/27 0027
 * modified by yihukurama in 下午 1:21 2023/7/27 0027
 */
@Api(value = "Public API", tags = "公共API")
@RestController
@RequestMapping("/public")
@CrossOrigin(origins = "*", maxAge = 3600)
public class PublicController  {

    @Autowired
    RedisUtils redisUtils;
    @Autowired
    IPublicApi iPublicApi;

    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody Request<User> request) throws Exception {

        HttpServletRequest httpRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();


        return iPublicApi.login(request,httpRequest);
    }

    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public Result isLogin(@RequestBody Request<User> request) throws Exception {



        return iPublicApi.isLogin(request);
    }

    /**
     * 生成验证码
     */
    @GetMapping("/code")
    public Result getCode(HttpServletResponse response) throws IOException
    {
        Result ajax = Result.failed();

        // 保存验证码信息
        String verifyKey = UUID.randomUUID().toString().replace("-","");

        String capStr = null, code = null;
        BufferedImage image = null;

        // 生成验证码
        // 设置响应头信息
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");

        // 生成验证码文本并存储到session中
        String capText = defaultKaptcha.createText();

        redisUtils.set(verifyKey,capText,60L);
        // 根据文本生成图像
        BufferedImage bi = defaultKaptcha.createImage(capText);
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();

        // 将图像写入输出流
        ImageIO.write(bi, "jpg", os);

        try {
            os.flush();
        } finally {
            os.close();
        }

        CodeDTO codeDTO = new CodeDTO();
        codeDTO.setUuid(verifyKey);
        codeDTO.setImg(Base64.encode(os.toByteArray()));

        return Result.successed(codeDTO);
    }

}

